import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class HttpServer {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        if (args.length > 0) {
            try {
                serverSocket = new ServerSocket(Integer.parseInt(args[0]));
                 socket = serverSocket.accept();
                // System.out.println(socket);

            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                serverSocket = new ServerSocket(80);
                 socket = serverSocket.accept();
                // System.out.println(socket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        service(socket);

    }

    public static void service(Socket socket) {
        try {
            InputStream inputStream = socket.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String line = null;
            line = bufferedReader.readLine();
            System.out.println("请求行");
            System.out.println(line);
            String[] requestLine = line.split(" ");

            System.out.println("请求头");
            while (!(line = bufferedReader.readLine()).isEmpty()) {
                System.out.println(line);
            }
            System.out.println("空行");
            System.out.println(line);

            if (requestLine[0].equals("POST")) {
                System.out.println("请求数据");
                while (!(line = bufferedReader.readLine()).isEmpty()) {
                    System.out.println(line);
                }
            }



            OutputStream outputStream = socket.getOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);

            System.out.println("请求资源" + requestLine[1]);
            // 状态行
            bufferedWriter.write("HTTP/1.1 200 OK");
            bufferedWriter.write("\r\n");
            if (requestLine[1].equals("/")) {
                // 响应头
                bufferedWriter.write("Content-Type: text/html; charset=utf-8");
                bufferedWriter.write("\r\n");

                // 空行
                bufferedWriter.write("\r\n");

                // bufferedWriter.write("您好! 我在编写 Java 版 Web 服务器");
                FileReader fileReader = new FileReader("web/index.html");
                BufferedReader reader = new BufferedReader(fileReader);
                String content = null;
                while ((content = reader.readLine()) != null) {
                    bufferedWriter.write(content);
                }

                reader.close();
                fileReader.close();
            }
            if (requestLine[1].equals("/favicon.ico")) {
                // 响应头
                bufferedWriter.write("Content-Type: image/x-icon");
                // bufferedWriter.write("\r\n");
                // bufferedWriter.write("Content-Length: 1966");
                bufferedWriter.write("\r\n");

                // 空行
                bufferedWriter.write("\r\n");

                // bufferedWriter.write("您好! 我在编写 Java 版 Web 服务器");
                FileInputStream fileInputStream = new FileInputStream("web/favicon.ico");
                InputStreamReader inputStreamReader1 = new InputStreamReader(fileInputStream);
                BufferedReader reader = new BufferedReader(inputStreamReader1);
                String content = null;
                while ((content = reader.readLine()) != null) {
                    bufferedWriter.write(content);
                }

                reader.close();
                inputStreamReader1.close();
                fileInputStream.close();

            }


            bufferedWriter.flush();

            bufferedWriter.close();
            outputStreamWriter.close();
            outputStream.close();
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();

            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
